package com.cw.service.impl;

import com.cw.common.constant.Constants;
import com.cw.common.constant.Response;
import com.cw.common.method.CommonMethod;
import com.cw.entity.ExtInformation;
import com.cw.entity.ThisIiInmaininfo;
import com.cw.entity.ThisIiPrepayin;
import com.cw.mapper2.ExtInformationMapper;
import com.cw.mapper2.PatientBedMiddleMapper;
import com.cw.mapper2.ThisIiPrepayinMapper;
import com.cw.request.NoCheckBedRequest;
import com.cw.service.IExtInformationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cw.util.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ly
 * @since 2022-03-16
 */
@Service
public class ExtInformationServiceImpl extends ServiceImpl<ExtInformationMapper, ExtInformation> implements IExtInformationService {

    @Autowired
    private ExtInformationMapper extInformationMapper;

    @Autowired
    private ThisIiPrepayinMapper thisIiPrepayinMapper;

    @Autowired
    private PatientBedMiddleMapper patientBedMiddleMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Response addNonHeaderCalibration(String inpatientNo) {
        try {
            List<String> inpatientList = Arrays.asList(inpatientNo.split(","));
            NoCheckBedRequest inpatientNoList =new NoCheckBedRequest();
            inpatientNoList.setInpatientNo(inpatientList);
            Date currDate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            List<ExtInformation> insertList = new ArrayList<>();
            CommonMethod commonMethod = new CommonMethod();
            //获得住院证号,拿到住院证号去查询Prepayin表里去查患者的preid
            List<ThisIiInmaininfo> getbedCheckList = thisIiPrepayinMapper.getbedCheck(inpatientNoList);
//            test
//            ThisIiInmaininfo testInfo = new ThisIiInmaininfo();
//            testInfo.setInpatientNo("ZY199803251888");
//            testInfo.setPreid("-28");
//            testInfo.setName("徐宁达测试9");
//            getbedCheckList.add(testInfo);
            List<BigDecimal> listPreId = getbedCheckList.stream().map(t -> BigDecimal.valueOf(Double.valueOf(t.getPreid()))).collect(Collectors.toList());
            getbedCheckList.forEach(item -> {
                ExtInformation entity = new ExtInformation();
                //消息通知类型
                entity.setMessageTpye("NonHeaderCalibration");
                entity.setExtendField1(item.getPreid() != null ? item.getPreid() : "");  //PreID
                entity.setExtendField2(item.getName() != null ? item.getName() : "");  //患者姓名
                entity.setExtendField3(item.getNurseCellCode() != null ? item.getNurseCellCode() : "");  //病区编号
                entity.setExtendField4(item.getNurseCellName() != null ? item.getNurseCellName() : "");  //病区名称
                entity.setExtendField5(item.getDeptCode() != null ? item.getDeptCode() : "");   //科室代码
                entity.setExtendField6(item.getDeptName() != null ? item.getDeptName() : "");   //科室名称
                String bedNo = item.getBedNo() == null ? "" : commonMethod.getBedName(item.getBedNo() != null ? item.getPreid() : "");
                entity.setExtendField7(bedNo);      //床位号
                entity.setExtendField8("");
                entity.setExtendField9("");
                //创建人
                entity.setCreatedBy("医惠调用");
                //创建时间
                entity.setCreatedTime(sdf.format(currDate));
                //字段备注
                entity.setMessageTypeRemark("ExtendField1为PreIdExtendField2为患者姓名ExtendField3为病区代码ExtendField4为病区名称ExtendField5为科室代码ExtendField6为科室名称ExtendField7为床位号");
                insertList.add(entity);
            });

            // Middle状态赋值取消4
            patientBedMiddleMapper.updateReleaseBed(listPreId);
            //  预约表床位号赋空
            thisIiPrepayinMapper.updateBedNo(listPreId);
//            插入记录信息表  EXT_INFORMATION
            extInformationMapper.addNonHeaderCalibration(insertList);
        } catch (Exception ex) {
            return Response.error(String.join(":", "插入失败:", ex.getMessage()));
        }
        return Response.success("插入成功");
    }
}
